var Article = require('../models/article')

module.exports = {

  add: function (req, res) {
    var body = req.body

    if (!body.title || !body.url || !body.desc || !body.order || !body.type) {
      return res.json({code: 122, msg: '内容不全'})
    }
    body.order = + body.order

    Article.create(body, function (err, article) {
      if (err) {
        console.log('articleController add fail. err = ' + err)
        return res.json({code: 177, msg: '添加失败'})
      }
      //return res.json({code: 100, data: {article: article}})
      res.redirect('/admin')
    })
  },

  getAll: function (req, res) {

    Article.getAll(function (err, articles) {
      if (err) {
        console.log('articleController getAll fail. err = ' + err)
        return res.json({code: 177, msg: '操作失败'})
      }
      return res.json({code: 100, data: {
        list: articles
      }})
    })
  },
  remove: (req, res) => {
    Article.remove({_id: req.query.id}, (err) => {
      res.json({
        code: 100,
        data: {}
      })
    })
  }
}